#include "LevelAffixes.h"

LevelAffixes::LevelAffixes(LevelAffixes* prev, vector<const Affix*> initAffixes) : previous(prev), affixes(initAffixes) {
}

unsigned long LevelAffixes::totalSize() const {
	if ( previous )
		return affixes.size() + previous->totalSize();
	else
		return affixes.size();
}

unsigned long LevelAffixes::levelSize() const {
	return affixes.size();
}

bool LevelAffixes::addAffix(const Affix* newAffix) {
	try {
		affixes.push_back(newAffix);
		return true;
	}
	catch (...) {
		return false;
	}
}

bool LevelAffixes::addAffixes(vector<const Affix*> newAffixes) {
	for ( vector<const Affix*>::iterator i = newAffixes.begin(); i != newAffixes.end(); ++i ) {
		try {
			affixes.push_back(*i);
		}
		catch (...) {
			return false;
		}
	}
	return true;
}

LevelAffixes::Iterator::Iterator(const LevelAffixes* LevAff, bool LtEq) : baseLevel(LevAff), currLevel(LevAff), calcLevelLtEq(LtEq), end(false) {
	*this = baseLevel->affixes.begin();
}

LevelAffixes::Iterator& LevelAffixes::Iterator::operator = (vector<const Affix*>::const_iterator right) {
	vector<const Affix*>::const_iterator::operator = (right);
	return *this;
}

LevelAffixes::Iterator& LevelAffixes::Iterator::start() {
	return *this = baseLevel->affixes.begin();
}

bool LevelAffixes::Iterator::isEnd() const {
	return end;
}

LevelAffixes::Iterator& LevelAffixes::Iterator::operator++() {
	vector<const Affix*>::iterator::operator++();
	if ( *this == currLevel->affixes.end() ) {
		if ( calcLevelLtEq && (currLevel = currLevel->previous) != NULL )
			*this = currLevel->affixes.begin();
		else
			end = true;
	}
	return *this;
}

LevelAffixes::Iterator& LevelAffixes::Iterator::operator++(int) {
	vector<const Affix*>::iterator::operator++();
	if ( *this == currLevel->affixes.end() ) {
		if ( calcLevelLtEq && (currLevel = currLevel->previous) != NULL )
			*this = currLevel->affixes.begin();
		else
			end = true;
	}
	return *this;
}
